package com.bingo.cf.certification.server.api.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bingo.cf.certification.server.api.dao.DemoTableMapper;
import com.bingo.cf.certification.server.api.entity.DemoTable;
import com.bingo.cf.certification.server.api.service.IDemoTableService;
import com.tip.restful.core.service.ServiceBaseImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Auto-Generated
 * @since 2018-12-17
 */
@Service
public class DemoTableServiceImpl extends ServiceBaseImpl<DemoTableMapper, DemoTable> implements IDemoTableService {
	Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 分页查询-服务实现
	 *
	 * @param page
	 * @param state
	 * @return
	 */
	@Override
	@Transactional
	public Page<HashMap<String, Object>> selectDemoPage1(Page<HashMap<String, Object>> page, Integer state) {
		return page.setRecords(baseMapper.selectDemoPage1(page, state));
	}

	@Override
	public Page<HashMap<String, Object>> selectDemoPage2(Page<HashMap<String, Object>> page,
			EntityWrapper<HashMap<String, Object>> ew) {

		return page.setRecords(baseMapper.selectDemoPage2(page, ew));
	}

	@Override
	public Page<DemoTable> selectDemoPage3(Page<DemoTable> page, EntityWrapper<DemoTable> ew) {
		return page.setRecords(baseMapper.selectDemoPage3(page, ew));
	}

	/**
	 * cacheNames 设置缓存的值 key：指定缓存的key，这是指参数id值。 key可以使用spEl表达式
	 *
	 * @param id
	 * @return
	 */
	@Cacheable(cacheNames = "demo1", key = "#id")
	public DemoTable getCacheDemo1(String id) {
		return super.selectById(id);
	}

	/**
	 * 如果设置sync=true， 如果缓存中没有数据，多个线程同时访问这个方法，则只有一个方法会执行到方法，其它方法需要等待
	 * 如果缓存中已经有数据，则多个线程可以同时从缓存中获取数据
	 *
	 * @param id
	 * @return
	 */
	@Cacheable(cacheNames = "demo2", sync = true)
	public DemoTable getCacheDemo2(String id) {
		return super.selectById(id);
	}

	/**
	 * allEntries = true: 清空demo1里的所有缓存
	 */
	@CacheEvict(cacheNames = "demo1", allEntries = true)
	public void clearcacheDemoAll() {
		logger.info("clearAll");
	}

	/**
	 * 对符合key条件的记录从缓存中demo1移除
	 */
	@CacheEvict(cacheNames = "demo1", key = "#id")
	public void delCacheDemo(String id) {
		logger.info("del cache demo");
	}

}
